// SelectionWnd.cpp : implementation file
//

#include "stdafx.h"
#include "MsnCoreViewer.h"
#include "SelectionWnd.h"


// CSelectionWnd

IMPLEMENT_DYNAMIC(CSelectionWnd, CWnd)

CSelectionWnd::CSelectionWnd()
{
	m_color = 0;
}

CSelectionWnd::~CSelectionWnd()
{
}


BEGIN_MESSAGE_MAP(CSelectionWnd, CWnd)
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CSelectionWnd message handlers
BOOL CSelectionWnd::Create()
{
	//OutputDebugString("CSelectionWnd::Create\r\n");
	LPCTSTR szCls = AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW,AfxGetApp()->LoadStandardCursor(IDC_ARROW),0,0);
	if (!CWnd::CreateEx(0,szCls,0,WS_POPUP|WS_CLIPSIBLINGS,0,0,0,0,0,0,0))
		return FALSE;
	return TRUE;
}

void CSelectionWnd::Show(HWND hParent, const RECT & rect)
{
	//OutputDebugString("CSelectionWnd::Show\r\n");
	HWND hWnd = GetSafeHwnd();
	if (!hWnd)
	{
		Create();
		hWnd = GetSafeHwnd();
	}
	else if (!IsWindow(hWnd))
	{
		Detach();
		Create();
		hWnd = GetSafeHwnd();
	}
	::SetParent(hWnd,hParent);
	::SetWindowPos(hWnd,HWND_TOP,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,SWP_NOACTIVATE);
	CRect smallerrect;
	CRect largerrect;
	CopyRect(&m_rect,&rect);
	CopyRect(&largerrect,&rect);
	largerrect.right = largerrect.Width();
	largerrect.bottom = largerrect.Height();
	largerrect.top =0;
	largerrect.left = 0;
	//ClientToScreen(&largerrect);

	//::ScreenToClient(::GetParent(hParent),(LPPOINT)(LPRECT)&largerrect);
	//::ScreenToClient(::GetParent(hParent),((LPPOINT)(LPRECT)&largerrect)+1);
	smallerrect = largerrect;
	smallerrect.DeflateRect(2,2);
	HRGN hOuterRgn = ::CreateRectRgn(largerrect.left,largerrect.top,largerrect.right,largerrect.bottom);
	HRGN hInnerRgn = ::CreateRectRgn(smallerrect.left,smallerrect.top,smallerrect.right,smallerrect.bottom);
	HRGN hNewRgn = CreateRectRgn(0,0,0,0);
	CombineRgn(hNewRgn,hOuterRgn,hInnerRgn,RGN_XOR);
	DeleteObject(hOuterRgn);
	DeleteObject(hInnerRgn);
	SetWindowRgn(hNewRgn,FALSE);
	ShowWindow(SW_SHOWNOACTIVATE);
	AfxGetApp()->GetMainWnd()->SetForegroundWindow();
}

void CSelectionWnd::Hide()
{
	//OutputDebugString("CSelectionWnd::Hide\r\n");
	HWND hWnd = GetSafeHwnd();
	if (hWnd && IsWindow(hWnd))
	{
		ShowWindow(SW_HIDE);
	}
}

void CSelectionWnd::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	
	CRect rect;
	GetClientRect(rect);
	dc.FillSolidRect(rect,m_color);
}

void CSelectionWnd::SetColor(COLORREF crColor)
{
	m_color = crColor;
	if (GetSafeHwnd() && IsWindow(GetSafeHwnd()))
	{
		Invalidate(FALSE);
	}
}